import java.awt.Color;
import java.awt.Component;
import java.util.EventObject;

import javax.swing.AbstractCellEditor;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

public class SpreadSpinner extends AbstractCellEditor implements
		TableCellEditor, TableCellRenderer {

	private JSpinner editSpinner, renderSpinner;
	private Border originalBorder;
	SpinnerNumberModel m_model = new SpinnerNumberModel(0.00,Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 0.05);

	@Override
	public Object getCellEditorValue() {
		// TODO Auto-generated method stub
		return editSpinner.getValue();
	}

	@Override
	public Component getTableCellEditorComponent(JTable table, Object value,
			boolean isSelected, int row, int column) {
		// TODO Auto-generated method stub
		if (value != null) {
			editSpinner.setValue(value);
		}
		return editSpinner;
	}

	public SpreadSpinner(double value, Number stepsize) {
		editSpinner = new JSpinner();
		renderSpinner = new JSpinner();
		m_model.setStepSize(stepsize);
		editSpinner.setValue(value);
		editSpinner.setModel(m_model);
		originalBorder = editSpinner.getBorder();
		editSpinner.setBorder(new LineBorder(Color.BLUE));
	}

	public SpreadSpinner() {
		editSpinner = new JSpinner(m_model);
		renderSpinner = new JSpinner();
		originalBorder = editSpinner.getBorder();
		editSpinner.setBorder(new LineBorder(Color.BLUE));
	}
	
	public boolean isCellEditable(EventObject evt) {
		return true;
	}

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		// TODO Auto-generated method stub
		if (hasFocus) {
			renderSpinner.setBorder(new LineBorder(Color.BLUE));
		} else {
			renderSpinner.setBorder(originalBorder);
		}
		renderSpinner.setValue(value);
		return renderSpinner;
	}
}
